package org.dromara.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.service.wx.WxUserTranslationService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.wx.config.DefaultWxUserConfig;
import org.dromara.wx.domain.WxUserRole;
import org.dromara.wx.mapper.WxUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.dromara.common.core.utils.MapstructUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.dromara.wx.domain.WxUser;
import org.dromara.wx.domain.bo.WxUserBo;
import org.dromara.wx.domain.query.WxUserQuery;
import org.dromara.wx.domain.vo.WxUserVo;
import org.dromara.wx.mapper.WxUserMapper;
import org.dromara.wx.service.IWxUserService;

import java.util.Collection;
import java.util.List;

/**
 * 微信用户Service业务层处理
 *
 * @author yf
 * @date 2024-06-05
 */
@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements IWxUserService, WxUserTranslationService {

    @Autowired
    private DefaultWxUserConfig wxUserConfig;

    @Autowired
    private WxUserRoleMapper userRoleMapper;
    /**
     * 查询微信用户
     *
     * @param id 主键
     * @return WxUserVo
     */
    @Override
    public WxUserVo queryById(Integer id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询微信用户列表
     *
     * @param query 查询对象
     * @return WxUserVo
     */
    @Override
    public TableDataInfo<WxUserVo> queryPageList(WxUserQuery query) {
        return PageQuery.of(() -> baseMapper.queryList(query));
    }

    /**
     * 查询微信用户列表
     *
     * @param query 查询对象
     * @return WxUserVo
     */
    @Override
    public List<WxUserVo> queryList(WxUserQuery query) {
        return baseMapper.queryList(query);
    }

    /**
     * 根据新增业务对象插入微信用户
     *
     * @param bo 微信用户新增业务对象
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(WxUserBo bo) {
        WxUser add = MapstructUtils.convert(bo, WxUser.class);
        return save(add);
    }

    /**
     * 根据编辑业务对象修改微信用户
     *
     * @param bo 微信用户编辑业务对象
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(WxUserBo bo) {
        WxUser update = MapstructUtils.convert(bo, WxUser.class);
        return updateById(update);
    }

    /**
     * 校验并批量删除微信用户信息
     *
     * @param ids 主键集合
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Integer> ids) {
        return removeByIds(ids);
    }

    /**
     * 翻译微信用户信息
     *
     * @param id 主键
     * @return WxUserVo
     */
    @Override
    @Cacheable(cacheNames = WxUser.TRANS_ID_TO_VO, key = "#id")
    public Object selectObjectById(Integer id){
        return baseMapper.selectVoById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public WxUserVo createDefaultVo(String openid) {
        WxUser wxUser = new WxUser();
        wxUser.setOpenId(openid);
        wxUser.setNickname(wxUserConfig.getDefaultNickname());
        wxUser.setAvatarImage(wxUserConfig.getDefaultAvatar());
        wxUser.setSignature(wxUserConfig.getDefaultSignature());
        wxUser.setUserType(wxUserConfig.getDefaultUserType());

        //校验用户类型
        UserType.getUserType(wxUser.getUserType());
        int wxuserId = baseMapper.insert(wxUser);
        WxUserRole wxUserRole = new WxUserRole();
        wxUserRole.setRoleId(wxUserConfig.getDefaultRoleId());
        wxUserRole.setUserId(wxuserId);
        userRoleMapper.insert(wxUserRole);
        return MapstructUtils.convert(wxUser, WxUserVo.class);
    }

    @Override
    public WxUserVo selectWxUserByPhonenumber(String phonenumber) {
        return baseMapper.selectWxUserWithRolesByphoneNumber(phonenumber);
    }

    @Override
    public WxUserVo selectWxUserByOpenId(String openid) {
        LambdaQueryWrapper<WxUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(openid),WxUser::getOpenId,openid);
        return baseMapper.selectVoOne(lqw);
    }
}
